package com.tstudy.laoshibang.active;

import java.util.List;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.mode.Dictionary;
import com.tstudy.laoshibang.utils.CommonUtil;

import android.annotation.SuppressLint;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
 * 字典列表adapter
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年6月3日 上午11:24:26
 */
@SuppressLint("RtlHardcoded")
public class DictionaryAdapter extends BaseAdapter{
	LayoutInflater mInflater;
	private List<Dictionary> mItems;
	int spHeight = CommonUtil.dip2px(1);
	public DictionaryAdapter() {
		 mInflater = LayoutInflater.from(BaseApplication.mContext);
	}
	
	int mSelectColor = BaseApplication.getResColor(R.color.common_bg);
	int mUnselectColor = BaseApplication.getResColor(R.color.whiteColor);
	
	@Override
	public int getCount() {
		return mItems == null ? 0:mItems.size();
	}
	
	public void setData(List<Dictionary> items) {
		setData(items,true);
	}
	public void setData(List<Dictionary> items,boolean isRefresh) {
		mItems = items;
		if (isRefresh) {
			notifyDataSetChanged();
		}
	}
	
	public List<Dictionary> getData() {
		return mItems;
	}
	

	@Override
	public Object getItem(int position) {
		return mItems == null ? null:mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if(convertView ==  null){
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.dictionary_item, null);
			viewHolder.name = (TextView) convertView.findViewById(R.id.dictionary_item_name);
			viewHolder.sp = (View) convertView.findViewById(R.id.dictionary_item_sp);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if(viewHolder != null){
			setConvertView(viewHolder,position,convertView);
		}
//		convertView.setOnClickListener(null);
		return convertView;
	}
	
	public void setConvertView(final ViewHolder viewHolder,final int position,View view){
		final Dictionary dictionary = (Dictionary) getItem(position);
		viewHolder.name.setText(dictionary.name);
		if (dictionary.isSelected) {
			viewHolder.name.setBackgroundColor(mSelectColor);
		}else{
			viewHolder.name.setBackgroundColor(mUnselectColor);
		}
		if (Dictionary.TYPE_CITY.equals(dictionary.type) || Dictionary.TYPE_SCHOOL.equals(dictionary.type)) {
			viewHolder.name.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
			viewHolder.name.setPadding(CommonUtil.dip2px(10), 0, 0, 0);
		}else{
			viewHolder.name.setGravity(Gravity.CENTER);
			viewHolder.name.setPadding(0, 0, 0, 0);
		}
		if (position == mItems.size()-1) {
			viewHolder.sp.getLayoutParams().height = 1;
		}else{
			viewHolder.sp.getLayoutParams().height = spHeight;
		}
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mActionCallBack != null) {
					mActionCallBack.onItemClick(dictionary,position);
				}
			}
		});
	}
	
	
	static class ViewHolder{
		TextView name;
		View sp;
	}
	
	public interface ActionCallBack{
		public void onItemClick(Dictionary dictionary,int position);
	}
	ActionCallBack mActionCallBack;
	public void setActionCallBack(ActionCallBack actionCallBack){
		mActionCallBack = actionCallBack;
	}
}
